<template>
  <div class="reg">
    <div class="box">
      <div class="head">
        <img src="@/assets/images/login_title.png" alt="" />
      </div>
      <el-form :model="form" :rules="rules" ref="form">
        <el-form-item prop="username">
          <el-input
            placeholder="请输入用户名"
            prefix-icon="el-icon-user"
            v-model="form.username"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            placeholder="请输入密码"
            prefix-icon="el-icon-lock"
            v-model="form.password"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item prop="repassword">
          <el-input
            placeholder="请确认密码"
            prefix-icon="el-icon-lock"
            v-model="form.repassword"
            show-password
          ></el-input>
        </el-form-item>
        <el-button
          type="primary"
          style="width:100%"
          @click="submit"
          :loading="loading"
          >注册</el-button
        >
      </el-form>
      <span class="go-login" @click="$router.push('/login')">登陆</span>
    </div>
  </div>
</template>

<script>
import { apiReg } from '@/api/reg.js'
export default {
  data () {
    return {
      loading: false,
      form: {
        username: '',
        password: '',
        repassword: ''
      },
      rules: {
        username: [
          { required: true, message: '必填', trigger: 'change' },
          {
            validator: (rule, value, callback) => {
              const reg = /^[a-zA-Z0-9]{1,10}$/
              reg.test(value)
                ? callback()
                : callback(new Error('用户名字母数字,长度1-10'))
            }
          }
        ],
        password: [
          { required: true, message: '必填', trigger: 'change' },
          {
            min: 6,
            max: 15,
            message: '非空字符串,长度6-15',
            trigger: 'change'
          }
        ],
        repassword: [
          { required: true, message: '必填', trigger: 'change' },
          {
            validator: (rule, value, callback) => {
              if (value === this.form.password) {
                callback()
              } else {
                callback(new Error('两次密码不一致'))
              }
            }
          }
        ]
      }
    }
  },
  methods: {
    submit () {
      this.$refs.form.validate(result => {
        console.log('表单验证结果', result)
        if (result) {
          this.loading = true
          apiReg(this.form)
            .then(res => {
              // console.log('注册返回值', res)
              // this.loading = false
              // if (res.code === 0) {
              this.$message.success('注册成功')
              this.$router.push('/login')
              // } else {
              //   this.$message.error(res.message)
              // }
            })
            .finally(() => {
              this.loading = false
            })
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.reg {
  height: 100%;
  background: url('~@/assets/images/login_bg.jpg') center;
  background-size: cover;
  display: flex;
  justify-content: center;
  align-items: center;
  .box {
    width: 400px;
    background-color: #fff;
    padding: 0 25px;
    .head {
      text-align: center;
      padding: 10px 0;
    }
    .go-login {
      font-size: 12px;
      color: #ccc;
      line-height: 45px;
      cursor: pointer;
      &:hover {
        color: #000;
      }
    }
  }
}
</style>
